package pre.lxm.demo.chart.recycler;


import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import pre.lxm.demo.R;
import pre.lxm.demo.chart.bean.RecordBean;

public class RecordChartAdapter extends RecyclerView.Adapter<RecordChartAdapter.RecordChartVH> {

    private LayoutInflater mInflater;
    private List<RecordBean> mRecords;
    private Context mContext;

    public RecordChartAdapter(Context context, List<RecordBean> records) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mRecords = records;
    }

    @Override
    public RecordChartVH onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_record_recycler, parent, false);
        RecordChartVH recordVH = new RecordChartVH(view);
        recordVH.turnTv = (TextView) view.findViewById(R.id.tv_record_turn);
        recordVH.winTv = (TextView) view.findViewById(R.id.tv_record_win);
        recordVH.flatTv = (TextView) view.findViewById(R.id.tv_record_flat);
        recordVH.failureTv = (TextView) view.findViewById(R.id.tv_record_failure);
        return recordVH;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onBindViewHolder(RecordChartVH holder, int position) {
        RecordBean itemRecord = mRecords.get(position);
        holder.turnTv.setText(itemRecord.getTurn());
        String result = itemRecord.getResult();
        String host = itemRecord.getIsHost().equals("h") ? "主" : "客";
        holder.winTv.setVisibility(View.INVISIBLE);
        holder.failureTv.setVisibility(View.INVISIBLE);
        holder.flatTv.setVisibility(View.INVISIBLE);

        Drawable mDrawable;
        if (itemRecord.getIsHost().equals("h")) {
            //主
            mDrawable = mContext.getDrawable(R.drawable.host_bg);
        } else {
            //客
            mDrawable = mContext.getDrawable(R.drawable.guest_bg);
        }
        if (TextUtils.equals(result, "3")) {
            //胜
            holder.winTv.setText(host);
            holder.winTv.setVisibility(View.VISIBLE);
            holder.winTv.setBackground(mDrawable);
        } else if (TextUtils.equals(result, "1")) {
            //平
            holder.flatTv.setText(host);
            holder.flatTv.setVisibility(View.VISIBLE);
            holder.flatTv.setBackground(mDrawable);
        } else if (TextUtils.equals(result, "0")) {
            //负
            holder.failureTv.setText(host);
            holder.failureTv.setVisibility(View.VISIBLE);
            holder.failureTv.setBackground(mDrawable);
        }
    }


    public RecordBean getItemData(int position) {
        return mRecords.get(position);
    }

    public void refresh(List<RecordBean> newData) {
        mRecords.clear();
        mRecords.addAll(newData);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mRecords.size();
    }

    public static class RecordChartVH extends RecyclerView.ViewHolder {
        TextView turnTv;
        TextView winTv;
        TextView flatTv;
        TextView failureTv;


        public RecordChartVH(View itemView) {
            super(itemView);
        }
    }
}
